package com.sbeam.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sbeam.mapper.UserMapper;
import com.sbeam.mapper.UserMapper2;
import com.sbeam.pojo.Games;
import com.sbeam.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import sun.security.provider.MD5;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserMapper2 userMapper2;
    @Override
    public String findByUP(User user) {
        byte[] bytes = user.getPassword().getBytes();
        String md5Pw = DigestUtils.md5DigestAsHex(bytes);
        String username = user.getUsername();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                    .eq("password", md5Pw);
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1 == null){
            return null;
        }
        return UUID.randomUUID().toString().replace("-", "");
    }

    @Override
    public void addUser(User user) {
        String password = user.getPassword();
        String MD5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        user.setPassword(MD5Pass).setStatus(true);
        userMapper.insert(user);
    }

    @Override
    public String findUser(User user) {
        String username = user.getUsername();
        String phone = user.getPhone();
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username)
                .eq("phone", phone);
        User user1 = userMapper.selectOne(queryWrapper);
        if(user1 == null){
            return null;
        }
        return "success";
    }

    @Override
    public void updateUser(User user) {
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        String username = user.getUsername();
        String password = user.getPassword();
        String MD5Pass = DigestUtils.md5DigestAsHex(password.getBytes());
        wrapper.set("password", MD5Pass)
                .eq("username", username)
                .eq("phone", user.getPhone());
        userMapper.update(null, wrapper);
    }

    @Override
    public User getPersonInfo(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User findUserByName(String username) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        return userMapper.selectOne(wrapper);
    }

    @Override
    public void updateUser1(User user) {
        userMapper.updateById(user);
    }

    @Override
    public void updateUser2(User user) {
        byte[] bytes = user.getPassword().getBytes();
        String md5Pw = DigestUtils.md5DigestAsHex(bytes);
        user.setPassword(md5Pw);
        userMapper.updateById(user);
    }

    @Override
    public List<Games> getOrderList(String username) {
        QueryWrapper<User> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("username", username);
        String ordergames = userMapper.selectOne(wrapper1).getOrdergame();
        if (ordergames == null) return null;
        String [] gameList= ordergames.split(",");
        QueryWrapper<Games> wrapper = new QueryWrapper<>();
        wrapper.in("name", gameList);
        List<Games> games = userMapper2.selectList(wrapper);
        for (int i=0;i<games.size();i++){
            String[] split = games.get(i).getImages().split(",");
            ArrayList<String> strings = new ArrayList<>(Arrays.asList(split));
            games.get(i).setImagesList(strings);
        }
        return games;
    }

    @Override
    public void deleteByName(String username) {
        QueryWrapper<User> wrapper= new QueryWrapper<>();
        wrapper.eq("username", username);
        userMapper.delete(wrapper);
    }

}
